﻿using Newtonsoft.Json;
using System;

namespace MyPLC.Helper
{
    public class JsonHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonData"></param>
        /// <returns></returns>
        public static string SerializeObject(object obj)
        {
            try
            {
                return JsonConvert.SerializeObject(obj);
            }
            catch (Exception ex)
            {
                LoggerHelper.Error(string.Format("DeserializeObject_Error:{0}", ex.Message));
                return string.Empty;
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonData"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string jsonData)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(jsonData);
            }
            catch (Exception ex)
            {
                LoggerHelper.Error(string.Format("DeserializeObject_Error:{0}", ex.Message));
                return default(T);
            }
        }
    }
}
